//引入express 框架
const express = require("express");
const db = require("../modules/DBhelper");
const moment = require("moment");

//创建路由
let router = express.Router();

//加入购物车
router.post("/add", async function (request, response, next) {
    var carobj = request.body;
    let sql = "SELECT* FROM `cart` WHERE `u_id`=? AND `c_name`=? ;";
    let params = [
        carobj.uId,
        carobj.cname
    ];
    let result = await db.exec(sql, params);
    let isaddcart = result && result.length >= 1; //true表示已经购买过

    if (isaddcart) {
        let sqlupdate = " UPDATE `cart` SET `c_num`= c_num+? , `c_total`=c_num*c_price , `c_class`=? WHERE `u_id`=? AND `c_name`=?;";
        let paramsupdate = [
            carobj.cnum,
            carobj.ucls,
            carobj.uId,  
            carobj.cname,
          
        ];

        let resultupdate = await db.exec(sqlupdate, paramsupdate);
        let isupdate = resultupdate && resultupdate.affectedRows >= 1;

        let updatemessage = {
            msg: isupdate ? "加入购物车成功" : "加入购物车失败",
            status: isupdate ? 200 : -200,
            model: "update"
        }
        response.json(updatemessage);
    } else {
        let sqlinert = "INSERT INTO `cart` (`u_id`,`c_name`,`c_price`,`c_num`,`c_total`,`c_img`,`c_createtime`)  VALUES(?,?,?,?,?,?,?);";
        let paramsinsert = [
            carobj.uId,
            carobj.cname,
            carobj.cprice,
            carobj.cnum,
            carobj.cprice * carobj.cnum,
            carobj.cimg,
            carobj.ucls,
            moment().format("YYYY-MM-DD HH:mm:ss")
        ]

        let resultinsert = await db.exec(sqlinert, paramsinsert);

        let isinsert = resultinsert && resultinsert.affectedRows >= 1;

        let insertmessage = {
            msg: isinsert ? "加入购物车成功" : "加入购物车失败",
            status: isinsert ? 200 : -200,
            model: "insert"
        }
        response.json(insertmessage);
    }
})


router.get("/query", async function (request, response, next) {
    try {
        let userId = request.query.uId;
        let sql = " SELECT *FROM cart WHERE `u_id`=? AND `c_status`=1 ;"

        let resultQuery = await db.exec(sql, [userId]);

        response.json({ msg: "查询成功", status: 200, data: resultQuery })
    } catch (error) {
        response.json({ msg: "查询失败", status: -200, error })
    }
});


router.post("/modify", async function (request, response, next) {
    let params = [
        request.body.cnum,
        request.body.cid
    ]

    let sql = "UPDATE `cart` SET `c_num`=? ,`c_total`=c_num*`c_price` WHERE `c_id`=? ;";
    let result = await db.exec(sql, params);
    let ismodify = result && result.affectedRows >= 1;

    response.json({
        msg: ismodify ? "修改成功" : "修改失败",
        status: ismodify ? 200 : -200
    })
})

router.post("/delete", async function (request, response, next) {
    try {
        let params = [
            request.body.cid
        ]
        // let sql = " UPDATE `cart` SET `c_status`=0 WHERE c_id=? ;";
        let sql = "DELETE FROM `cart` WHERE c_id = ?;";

        let result = await db.exec(sql, params);

        let isdelete = result && result.affectedRows >= 1;

        response.json({
            msg: isdelete ? "删除成功" : "删除失败",
            status: isdelete ? 200 : -200
        })
    }catch(error){
        response.json({
            msg: "删除失败",
            status:-200,
            error
        })
    }

})















module.exports = {
    router
}